package com.microHR.hr.service.impl;

import com.microHR.hr.mapper.HrMapper;
import com.microHR.hr.mapper.HrRoleMapper;
import com.microHR.hr.model.Hr;
import com.microHR.hr.model.HrExample;
import com.microHR.hr.service.HrService;
import com.microHR.hr.util.HrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HrServiceImpl implements HrService {
    @Autowired
    private HrMapper hrMapper;
    @Autowired
    private HrRoleMapper hrRoleMapper;


    @Override
    public List<Hr> getAllHrs(String keywords) {
        return hrMapper.getAllHrs(HrUtils.getCurrentHr().getId(),keywords);
    }
    @Override
    public List<Hr> getAllHrsExceptCurrentHr() {
        return hrMapper.getAllHrsExceptCurrentHr(HrUtils.getCurrentHr().getId());
    }


    @Override
    public Integer updateHr(Hr hr) {
        return hrMapper.updateByPrimaryKeySelective(hr);
    }

    @Override
    public Integer updateHyById(Hr hr) {
        return hrMapper.updateByPrimaryKeySelective(hr);
    }

    @Override
    public Integer updateUserface(String url, Integer id) {
        return hrMapper.updateUserface(url, id);
    }

    @Override
    public boolean updateHrRole(Integer hrid, Integer[] rids) {
        hrRoleMapper.deleteByHrid(hrid);
        return hrRoleMapper.addRole(hrid, rids) == rids.length;
    }

    @Override
    public Integer deleteHrById(Integer id) {
        return hrMapper.deleteByPrimaryKey(id);
    }

    @Override
    public boolean updateHrPasswd(String oldpass, String pass, Integer hrid) {
        // 1. 构造 MBG 的 Example 对象（定义更新条件：id = hrid）
        HrExample example = new HrExample();
        HrExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(hrid); // 等价于 SQL: WHERE id = #{hrid}

        // 2. 查询当前用户信息（用于验证旧密码）
        Hr hr = hrMapper.selectByExample(example).get(0); // 根据 Example 条件查询用户
        if (hr == null) {
            return false; // 用户不存在
        }

        // 3. 验证旧密码
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (!encoder.matches(oldpass, hr.getPassword())) {
            return false; // 旧密码错误
        }

        // 4. 构造更新的 Hr 对象（仅设置密码字段）
        Hr hrToUpdate = new Hr();
        hrToUpdate.setPassword(encoder.encode(pass)); // 加密新密码

        // 5. 使用 MBG 的 updateByExampleSelective 执行更新（仅更新密码）
        int affectedRows = hrMapper.updateByExampleSelective(hrToUpdate, example);
        return affectedRows > 0; // 影响行数 > 0 表示更新成功
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Hr hr = hrMapper.loadUserByUsername(username);
        if (hr == null) {
            throw new UsernameNotFoundException("用户名不存在!");
        }
        hr.setRoles(hrMapper.getHrRolesById(hr.getId()));
        return hr;
    }
}
